package com.zto.sch.base.thread;

import org.junit.Test;
import org.springframework.util.StopWatch;

import java.math.BigDecimal;
import java.util.concurrent.*;

/**
 * @program: sch-diy-parent
 * @description:
 * @author: sch
 * @create: 2021-05-17 15:21
 **/
public class ThreadPollCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(50, 100, 5, TimeUnit.SECONDS, new ArrayBlockingQueue(1000));
        StopWatch stopwatch = new StopWatch("多线程");
        stopwatch.start();
        MyCallable myCallable = new MyCallable(1000000);
        Future<Integer> submit = executor.submit(myCallable);
//        System.out.println(submit.get());
        stopwatch.stop();
        System.out.println("耗时:" + stopwatch.getTotalTimeMillis() + "ms");
        System.out.println("线程池 over...");
    }

    @Test
    public void test() {
        StopWatch stopwatch = new StopWatch("单线程");
        stopwatch.start();
        for (int i = 0; i < 9000000; i++) {
            if (i % 10000 == 0) {
                System.out.println("单线程:" + i);
            }
        }
        stopwatch.stop();
        System.out.println("耗时：" + stopwatch.getTotalTimeMillis() + "ms");
        System.out.println("单线程over...");
    }

    @Test
    public void test2(){
        System.out.println(new BigDecimal(10).negate());
    }


    static class MyCallable implements Callable<Integer> {
        private Integer size;

        public MyCallable(Integer size) {
            this.size = size;
        }

        @Override
        public Integer call() throws Exception {
            int num = 0;
            for (int i = 1; i < size; i++) {
                if (i % 10000 == 0) {
                    System.out.println("多线程:" + i);
                }
                num += i;
            }
            return num;
        }
    }
}
